#include <time.h>
#include "myhead.h"
#include "lcd.h"
#include "ts.h"


extern const unsigned char gImage_load1[1152000];
extern const unsigned char gImage_load2[1152000];
extern const unsigned char gImage_load3[1152000];
extern const unsigned char gImage_load4[1152000];
extern const unsigned char gImage_load5[1152000];
extern const unsigned char gImage_load6[1152000];
extern const unsigned char gImage_load7[1152000];
extern const unsigned char gImage_load8[1152000];
extern const unsigned char gImage_load9[1152000];
extern const unsigned char gImage_load10[1152000];

#define SLIDE_RESPONSE_RATE 60

const unsigned char *photo[] =
{
	gImage_load1, gImage_load2,
	gImage_load7, gImage_load8
};

const unsigned char *cloud[] =
{
	gImage_load6, gImage_load8, 
	gImage_load9
};

const unsigned char *led[] =
{
	gImage_load2, gImage_load3, 
	gImage_load4, gImage_load5
};


void photo_album(void)
{
	// pthread_t tid;
	// pthread_create(&tid, NULL, detect_exit_sig, NULL);
	// pthread_detach(tid);

	int i = 0;
	int mode = 0;

	lcd_draw_bmp_horizontal(0, 0, 800, 480, photo[i]); 

	struct coordinate old_coor;			//触摸屏坐标
	struct coordinate new_coor;
	bool released;
	bool firsttouch  = true;			//第一手接触

	// bool doubletouch = false;		//判断是否有双击事件发生
	// time_t t_s, t_e;					//记录点击的时间
	// double interval;					//记录双击的时间间隔
	// int clicked_num = 0;				//记录已点击次数(0, 1, 2)

	while(1)
	{
		ts_trace(ts_fd, &old_coor, &released);
		printf("x = %d, y = %d, released = %d\n",
						 old_coor.x, old_coor.y, released);
		//划屏操作
		if (released==false && firsttouch
			&&old_coor.x < 765 && old_coor.x > 35)
		{
			while(!released)
			{
				ts_trace(ts_fd, &new_coor, &released);

				float anglex = (float)(new_coor.y-old_coor.y)
										/(new_coor.x-old_coor.x);

				printf("[new] x = %d, y = %d, anglex = %f\n",
								 new_coor.x, new_coor.y, anglex);

				if (new_coor.x == old_coor.x)
					continue;
				if (anglex<TAN30 && anglex>-TAN30
					&& (new_coor.x-old_coor.x)>SLIDE_RESPONSE_RATE)				//右划操作
				{
					printf("right====1\n", i);
					i = i >= 3 ? 0 : (i+1);				
					pic_ltor(photo[i]);	
					break;
				}
				if (anglex<-TAN30
					&& (new_coor.x-old_coor.x)>SLIDE_RESPONSE_RATE)				//右上滑操作
				{
					printf("right up====3\n");
					i = i >= 3 ? 0 : (i+1);				
					prismatic(photo[i]);
					break;
				}
				if (anglex>TAN30
					&& (new_coor.x-old_coor.x)>SLIDE_RESPONSE_RATE)				//右下滑操作
				{
					printf("right down====2\n");
					i = i >= 3 ? 0 : (i+1);				
					oblique(photo[i]);
					break;
				}
				if (anglex<-TAN30
					&& (old_coor.x-new_coor.x)>SLIDE_RESPONSE_RATE)				//左下滑操作
				{
					printf("left down====4\n");
					i = i >= 3 ? 0 : (i+1);				
					pic_utod(photo[i]);
					break;
				}
				if (anglex>TAN30
					&& (old_coor.x-new_coor.x)>SLIDE_RESPONSE_RATE)				//左上滑操作
				{
					printf("left up====5\n");
					i = i >= 3 ? 0 : (i+1);				
					rount(photo[i]);
					break;
				}				
			}
		}

		firsttouch = false;
		if (released)
			firsttouch = true;

		if (released && old_coor.x < 35 && old_coor.y < 35)
		{
			if (mode)
			{
				mode = 0;
				i = 0;
				pic_storied(photo[i], led[i]);
				printf("led=====6\n");
				while(i != 3)
				{
					ts_trace(ts_fd, &old_coor, &released);
					printf("[led]x = %d, y = %d, released = %d\n",
						 old_coor.x, old_coor.y, released);
					if (released)
					{
						i++;
						lcd_draw_bmp_horizontal(0, 0, 800, 480, led[i]); 
					}
				}
				printf("ledend=====6\n");
			}
			else
			{
				mode = 1;
				i = 0;
				pic_r_l_u_d(photo[i], cloud[i]);
				printf("cloud=====7\n");
				while(i != 2)
				{
					ts_trace(ts_fd, &old_coor, &released);
					printf("[cloud]x = %d, y = %d, released = %d\n",
						 old_coor.x, old_coor.y, released);
					if (released)
					{
						i++;
						lcd_draw_bmp_horizontal(0, 0, 800, 480, cloud[i]);
					}
				}
				printf("cloudend=====7\n");
			}
			i = 4;
		}

		if (released == true && old_coor.x > 765 && old_coor.y < 35)
			return;
	}
}
